package com.zk.yygh.dict.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zk.yygh.dict.mapper.DictMapper;
import com.zk.yygh.model.cmn.Dict;
import com.zk.yygh.vo.cmn.DictEeVo;

/**
 * @author: 张凯
 * @date: 2022/5/22 - 22:25
 */
public class DictListener extends AnalysisEventListener<DictEeVo> {

    //聚合baseMapper
    DictMapper dictMapper;
    public DictListener(DictMapper dictMapper){
        this.dictMapper=dictMapper;
    }

    //从导入文件的第二行开始一行一行的读
    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        //将读取dictEeVo转为Dict
        Dict dict = copyToDict(dictEeVo);
        //插入数据库
        dictMapper.insert(dict);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    //copyDictEeVo到Dict
    public Dict copyToDict(DictEeVo dictEeVo){
        Dict dict = new Dict();
        dict.setId(dictEeVo.getId());
        dict.setParentId(dictEeVo.getParentId());
        dict.setName(dictEeVo.getName());
        dict.setValue(dictEeVo.getValue());
        dict.setDictCode(dictEeVo.getDictCode());
        return dict;
    }
}
